package io.renren.modules.business.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.business.entity.RichtextContentEntity;
import io.renren.modules.business.service.RichtextContentService;
import io.renren.modules.business.vo.ContentVO;
import io.renren.modules.sys.shiro.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 富文本内容
 *
 * @author weifengbo
 * @email 931327348@qq.com
 * @date 2021-01-21 15:45:48
 */
@RestController
@Slf4j
@RequestMapping("business/richtextcontent")
public class RichtextContentController {
    @Autowired
    private RichtextContentService richtextContentService;

    @Autowired
    RedisUtils redisUtils;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:richtextcontent:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = richtextContentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:richtextcontent:info")
    public R info(@PathVariable("id") Long id) {
        RichtextContentEntity richtextContent = richtextContentService.getById(id);

        return R.ok().put("richtextContent", richtextContent);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:richtextcontent:save")
    public R save(@RequestBody ContentVO contentVO) {
        try {
            RichtextContentEntity richtextContent = new RichtextContentEntity();
            richtextContent.setTitle(contentVO.getTitle());
            richtextContent.setCategoryCode(contentVO.getCategoryCode());
            richtextContent.setContent(contentVO.getContent());
            Long userId = ShiroUtils.getUserId();
            Date date = new Date();
            richtextContent.setCreator(userId + "");
            richtextContent.setCreateTime(date);
            richtextContent.setUpdator(userId + "");
            richtextContent.setUpdateTime(date);
            richtextContent.setEnabled(1);
            richtextContent.setImageUrl(contentVO.getImageUrl());
            richtextContent.setDes(contentVO.getDes());
            richtextContent.setProjectName(contentVO.getProjectName());
            richtextContentService.save(richtextContent);

            return R.ok();
        } catch (Exception e) {
            log.error("保存内容失败", e);
            return R.error(1, "保存内容失败");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:richtextcontent:update")
    public R update(@RequestBody ContentVO contentVO) {
        try {
            RichtextContentEntity richtextContent = richtextContentService.getById(contentVO.getId());
            richtextContent.setImageUrl(contentVO.getImageUrl());
            richtextContent.setTitle(contentVO.getTitle());
            richtextContent.setCategoryCode(contentVO.getCategoryCode());
            richtextContent.setContent(contentVO.getContent());
            richtextContent.setUpdateTime(new Date());
            richtextContent.setUpdator(ShiroUtils.getUserId() + "");
            richtextContent.setDes(contentVO.getDes());
            richtextContent.setProjectName(contentVO.getProjectName());
            richtextContentService.updateById(richtextContent);
            String key = "content_" + richtextContent.getId().toString();
            redisUtils.delete(key);
            return R.ok();
        } catch (Exception e) {
            log.error("修改内容失败", e);
            return R.error(1, "修改内容失败");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:richtextcontent:delete")
    public R delete(@RequestBody Long[] ids) {
        richtextContentService.removeByIds(Arrays.asList(ids));
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                String key = "content_" + id.toString();
                redisUtils.delete(key);
            }
        }
        return R.ok();
    }

}
